package com.demo.controller;

import com.demo.entity.Carousel;
import com.demo.entity.Category;
import com.demo.entity.Product;
import com.demo.model.JsonMessage;
import com.demo.security.AuthUserDetail;
import com.demo.service.CarouselService;
import com.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Value("${load.upload}")
    private String uploadPath;
    @Autowired
    private ProductService service;

    @PostMapping("findAllProduct")
    @ResponseBody
    public JsonMessage findAllProduct() {
        JsonMessage message = new JsonMessage();
        List<Product> list = service.findAllProduct();
        message.setData(list);
        return message;
    }

    @PostMapping("findProductByCategoryId")
    @ResponseBody
    public JsonMessage findProductByCategoryId(Integer categoryId) {
        JsonMessage message = new JsonMessage();
        List<Product> list = service.findProductByCategoryId(categoryId);
        message.setData(list);
        return message;
    }

    @PostMapping("findAllCategory")
    @ResponseBody
    public JsonMessage findAllCategory() {
        JsonMessage message = new JsonMessage();
        List<Category> list = service.findAllCategory();
        message.setData(list);
        return message;
    }

    @PostMapping("insertProduct")
    @ResponseBody
    public JsonMessage insertProduct(@RequestBody Product product) {
        JsonMessage message = new JsonMessage();
        int id = service.insertProduct(product);
        message.setData(id);
        return message;
    }

    @PostMapping("updateProductPicture")
    @ResponseBody
    public JsonMessage updateProductPicture(@RequestBody List<String> picturePath) {
        JsonMessage message = new JsonMessage();
        if (picturePath.size() > 0) {
            service.updateProductPicture(Integer.parseInt(picturePath.get(picturePath.size() - 1)), picturePath);
        }
        message.setData("ok");
        return message;
    }


    @PostMapping("findProductById")
    @ResponseBody
    public JsonMessage findProductById(Integer productId) {
        JsonMessage message = new JsonMessage();
        Product product = service.findProductById(productId);
        message.setData(product);
        return message;
    }

    @PostMapping("updateProduct")
    @ResponseBody
    public JsonMessage updateProduct(@RequestBody Product product) {
        JsonMessage message = new JsonMessage();
        service.updateProduct(product);
        message.setData("ok");
        return message;
    }

    @PostMapping("deleteProductById")
    @ResponseBody
    public JsonMessage deleteProductById(Integer productId) {
        JsonMessage message = new JsonMessage();
        service.deleteProductById(productId);
        message.setData("ok");
        return message;
    }

    @GetMapping(value = "imgInfo", produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] imgInfo(String path) throws Exception {
        byte[] bs = null;
        File file = new File(uploadPath + File.separator + path);
        if (file.exists()) {
            FileInputStream inputStream = new FileInputStream(file);
            bs = new byte[inputStream.available()];
            inputStream.read(bs, 0, inputStream.available());
            return bs;
        }
        return bs;
    }

    //上传
    @PostMapping("pictureUpload")
    @ResponseBody
    public JsonMessage pictureUpload(@RequestParam("file") MultipartFile file) {
        JsonMessage message = new JsonMessage();
        // 获取原始名字
        String fileName = file.getOriginalFilename();

        // 获取后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件保存路径
        String path = uploadPath;
        String filePath = path + File.separator + fileName;
        fileName = filePath;

        // 文件对象
        File dest = new File(fileName);
        // 判断路径是否存在，如果不存在则创建
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            // 保存到服务器中
            file.transferTo(dest);
            message.setData(file.getOriginalFilename());
            return message;
        } catch (Exception e) {
            e.printStackTrace();
        }
        message.setData("上传失败");
        return message;
    }
}
